<?php 
	
	require_once('config.php');
	
	function GetProductByCatId($cat_id, $curPage=null, $rowPerPage=null)
	{
		if($curPage > 0)
			$curPage--;
		$from = $curPage * $rowPerPage;
		
		$sql = mysql_query("SELECT * FROM `cat_tb`, `product_tb`, `type_tb` 
							WHERE 	`product_tb`.`type_id` = `type_tb`.`type_id` AND 
									`type_tb`.`cat_id` = `cat_tb`.`cat_id` AND
									`cat_tb`.`cat_id` = $cat_id
									LIMIT $from, $rowPerPage	", dbconnect());
		
		$result = array(); 
		while ($product_row = mysql_fetch_assoc($sql)) { 
			$result[] = $product_row;
			} 
		
		return $result; 
	}
	function GetProductByTypeId($type_id, $curPage=null, $rowPerPage=null, $frontEnd=false)
	{
		if($curPage > 0)
			$curPage--;
		$from = $curPage * $rowPerPage;
		
		if($curPage == null && $rowPerPage == null)
		{
			
			if($frontEnd)
				$sql = "SELECT * FROM `product_tb`
						WHERE 	`product_tb`.`type_id` = $type_id AND
								`product_tb`.`num_supplier` > 0"; 
			else
				$sql = "SELECT * FROM `product_tb`
						WHERE 	`product_tb`.`type_id` = $type_id"; 
		}
		else
		{
			if($frontEnd)
			{
				$sql = "SELECT * FROM `product_tb`
						WHERE 	`product_tb`.`type_id` = $type_id AND `product_tb`.`num_supplier` > 0
						LIMIT $from, $rowPerPage";			
			}
			else
			{
				$sql = "SELECT * FROM `product_tb`
						WHERE 	`product_tb`.`type_id` = $type_id LIMIT $from, $rowPerPage";
			}
		}
		$sql = mysql_query($sql, dbconnect());
		
		$result = array(); 
		while ($product_row = mysql_fetch_assoc($sql)) { 
			$result[] = $product_row;
		} 
		
		return $result; 
	}
	function GetNumProductByTypeId($type_id, $frontEnd=false)
	{
		if($frontEnd)
			$sql = "SELECT count(product_id) as num FROM `product_tb`
							WHERE `product_tb`.`type_id` = $type_id AND
									`product_tb`.`num_supplier` > 0";
		else
			$sql = "SELECT count(product_id) as num FROM `product_tb`
							WHERE `product_tb`.`type_id` = $type_id";
		
		$sql = mysql_query($sql, dbconnect());
		$result = mysql_fetch_assoc($sql);
		return $result['num'];
	}
	function GetNumProductByCatId($cat_id)
	{
		$sql = "SELECT count(product_id) as num 
				FROM 	`product_tb`, `type_tb`, `cat_tb`
				WHERE 	`product_tb`.`type_id` = `type_tb`.`type_id` AND
						`type_tb`.`cat_id`= `cat_tb`.`cat_id` AND
						`cat_tb`.`cat_id` = $cat_id ORDER BY `product_tb`.`brand_id` ";
		
		$sql = mysql_query($sql, dbconnect());
		$result = mysql_fetch_assoc($sql);
		return $result['num'];
	}
	
	function GetProductRss($type=null)
	{
		if(!$type)
		{
			$sql = mysql_query("SELECT * FROM `product_tb` 
								ORDER BY `product_tb`.`product_date` DESC
								LIMIT 0, 20", dbconnect());
			$result = array(); 
			while ($product_row = mysql_fetch_assoc($sql))  
				$result[] = $product_row;

			return $result;
		}
		else
		{
		
		}
	}
	function GetProductById($id)
	{
		if($id == -1)
		{
			$sql = mysql_query("SELECT * FROM `product_tb` ORDER BY `product_tb`.`product_date` DESC",dbconnect());
			$result = array(); 
			while ($product_row = mysql_fetch_assoc($sql))  
				$result[] = $product_row;

			return $result;
		}
		else
		{
			$sql = mysql_query("SELECT * FROM `product_tb`, `brand_tb`, `type_tb`, `cat_tb`
								WHERE 	`product_tb`.`product_id` 	= $id AND
										`product_tb`.`brand_id`		= `brand_tb`.`brand_id` AND
										`product_tb`.`type_id` 		= `type_tb`.`type_id` AND 
										`type_tb`.`cat_id` 			= `cat_tb`.`cat_id`", dbconnect());
			
			$result = mysql_fetch_assoc($sql);
			if($result)
			{
				$result['product_des_full'] = stripcslashes($result['product_des_full']);
				$result['product_des_short'] = stripcslashes($result['product_des_short']);
				$result['product_name'] = stripcslashes($result['product_name']);
			}
			return $result;
		}
	}
	function GetProductReferById($product_id, $frontEnd=false)
	{
		if($frontEnd)
			$sql = mysql_query("SELECT * FROM `product_refer_tb`
								WHERE 	`product_refer_tb`.`product_id` = $product_id AND 
										`product_refer_tb`.`product_refer_status`>0
								ORDER BY `product_refer_order` DESC", dbconnect());
		else
			$sql = mysql_query("SELECT * FROM `product_refer_tb`
								WHERE 	`product_refer_tb`.`product_id` = $product_id 
								ORDER BY `product_refer_order` DESC", dbconnect());
		$result = array(); 
		while ($product_row = mysql_fetch_assoc($sql)) { 
			$result[] = $product_row;
		} 
		
		return $result; 
	}
	
	function GetNumProductSearch($type_id='', $brand_id='', $keyword='')
	{
		if($type_id == '')
		{
			if($brand_id == '')
			{
				if($keyword == '')
					$sql = "SELECT count(product_id) as num FROM `product_tb`";
				else
					$sql = "SELECT count(product_id) as num FROM `product_tb`
							WHERE `product_tb`.`product_name` like '%$keyword%'";
			}
			else
			{
				if($keyword == '')
					$sql = "SELECT count(product_id) as num FROM `product_tb`
							WHERE `product_tb`.`brand_id` = $brand_id";
				else
					$sql = "SELECT count(product_id) as num FROM `product_tb`
							WHERE 	`product_tb`.`brand_id` = $brand_id AND 
									`product_tb`.`product_name` like '%$keyword%'";
			}
		}
		else
		{
			if($brand_id == '')
			{
				if($keyword == '')
					$sql = "SELECT count(product_id) as num FROM `product_tb`
							WHERE `product_tb`.`type_id` = $type_id";
				else
					$sql = "SELECT count(product_id) as num FROM `product_tb`
							WHERE 	`product_tb`.`type_id` = $type_id AND
									`product_tb`.`product_name` like '%$keyword%'";
			}
			else
			{
				if($keyword == '')
					$sql = "SELECT count(product_id) as num FROM `product_tb`
							WHERE 	`product_tb`.`brand_id` = $brand_id AND
									`product_tb`.`type_id` = $type_id";
				else
					$sql = "SELECT count(product_id) as num FROM `product_tb`
							WHERE 	`product_tb`.`type_id` = $type_id AND
									`product_tb`.`brand_id` = $brand_id AND 
									`product_tb`.`product_name` like '%$keyword%'";
			}
		}
		
		$sql = mysql_query($sql, dbconnect());
		$result = mysql_fetch_assoc($sql);
		
		return $result['num'];
	}
	
	function GetProductSearch($curPage, $rowPerPage, $type_id='', $brand_id='', $keyword='')
	{
		if($curPage > 0)
			$curPage--;
		$from = $curPage * $rowPerPage;
		
		if($type_id == '')
		{
			if($brand_id == '')
			{
				if($keyword == '')
					$sql = "SELECT *  FROM `product_tb`, `brand_tb`, `type_tb` 
							WHERE `brand_tb`.`brand_id` = `product_tb`.`brand_id` AND
							`type_tb`.`type_id` = `product_tb`.`type_id`
							ORDER BY `product_tb`.`product_order` DESC, `product_tb`.`product_status` DESC,`product_tb`.`product_date` DESC
							LIMIT $from, $rowPerPage ";
				else
					$sql = "SELECT *  FROM `product_tb`, `brand_tb`, `type_tb`
							WHERE `product_tb`.`product_name` like '%$keyword%' AND
								`brand_tb`.`brand_id` = `product_tb`.`brand_id` AND
							`type_tb`.`type_id` = `product_tb`.`type_id` 
							ORDER BY `product_tb`.`product_order` DESC, `product_tb`.`product_date` DESC, `product_tb`.`product_date` DESC
							LIMIT $from, $rowPerPage";
			}
			else
			{
				if($keyword == '')
					$sql = "SELECT *  FROM `product_tb`, `brand_tb`, `type_tb`
							WHERE `product_tb`.`brand_id` = $brand_id AND
									`brand_tb`.`brand_id` = `product_tb`.`brand_id` AND
									`type_tb`.`type_id` = `product_tb`.`type_id` 
									ORDER BY `product_tb`.`product_order` DESC, `product_tb`.`product_date` DESC, `product_tb`.`product_date` DESC
									LIMIT $from, $rowPerPage";
				else
					$sql = "SELECT *  FROM `product_tb`, `brand_tb`, `type_tb`
							WHERE 	`product_tb`.`brand_id` = $brand_id AND 
									`product_tb`.`product_name` like '%$keyword%' 
									`brand_tb`.`brand_id` = `product_tb`.`brand_id` AND
									`type_tb`.`type_id` = `product_tb`.`type_id` 
									ORDER BY `product_tb`.`product_order` DESC, `product_tb`.`product_date` DESC, `product_tb`.`product_date` DESC
									LIMIT $from, $rowPerPage";
			}
		}
		else
		{
			if($brand_id == '')
			{
				if($keyword == '')
					$sql = "SELECT *  FROM `product_tb`, `brand_tb`, `type_tb`
							WHERE `product_tb`.`type_id` = $type_id AND
								`brand_tb`.`brand_id` = `product_tb`.`brand_id` AND
							`type_tb`.`type_id` = `product_tb`.`type_id` 
							ORDER BY `product_tb`.`product_order` DESC, `product_tb`.`product_date` DESC, `product_tb`.`product_date` DESC
							LIMIT $from, $rowPerPage";
				else
					$sql = "SELECT *  FROM `product_tb`, `brand_tb`, `type_tb`
							WHERE 	`product_tb`.`type_id` = $type_id AND
									`product_tb`.`product_name` like '%$keyword%' AND
									`brand_tb`.`brand_id` = `product_tb`.`brand_id` AND
									`type_tb`.`type_id` = `product_tb`.`type_id` 
									ORDER BY `product_tb`.`product_order` DESC, `product_tb`.`product_date` DESC, `product_tb`.`product_date` DESC
									LIMIT $from, $rowPerPage";
			}
			else
			{
				if($keyword == '')
					$sql = "SELECT *  FROM `product_tb`, `brand_tb`, `type_tb`
							WHERE 	`product_tb`.`brand_id` = $brand_id AND
									`product_tb`.`type_id` = $type_id AND
									`brand_tb`.`brand_id` = `product_tb`.`brand_id` AND
									`type_tb`.`type_id` = `product_tb`.`type_id` 
									ORDER BY `product_tb`.`product_order` DESC, `product_tb`.`product_date` DESC, `product_tb`.`product_date` DESC
									LIMIT $from, $rowPerPage";
				else
					$sql = "SELECT *  FROM `product_tb`, `brand_tb`, `type_tb`
							WHERE 	`product_tb`.`type_id` = $type_id AND
									`product_tb`.`brand_id` = $brand_id AND 
									`product_tb`.`product_name` like '%$keyword%' AND
									`brand_tb`.`brand_id` = `product_tb`.`brand_id` AND
									`type_tb`.`type_id` = `product_tb`.`type_id` 
									ORDER BY `product_tb`.`product_order` DESC, `product_tb`.`product_date` DESC, `product_tb`.`product_date` DESC
									LIMIT $from, $rowPerPage";
			}
		}
						
		//var_dump($sql); die();
		$sql = mysql_query($sql, dbconnect());
		
		$result = array(); 
		while ($product_row = mysql_fetch_assoc($sql)) { 
			$result[] = $product_row;
		} 
		
		return $result; 
	}
	
	function InsertProduct($arr)
	{
		$arr['product_des_short'] = addslashes($arr['product_des_short'] );
		$arr['product_des_full'] = addslashes($arr['product_des_full'] );
		$arr['product_name'] = addslashes($arr['product_name'] );
		
		$sql = "INSERT INTO `product_tb` 
					(	`product_name`, `type_id`, `brand_id`, 
						`product_img1`, `product_img2`, `product_img3`, `product_img4`, `product_img5`, 
						`product_des_short`, `product_des_full`) 
				VALUE ('{$arr['product_name']}', {$arr['type_id']}, {$arr['brand_id']} , 
						'{$arr['url1']}', '{$arr['url2']}', '{$arr['url3']}','{$arr['url4']}', '{$arr['url5']}',
						'{$arr['product_des_short']}', '{$arr['product_des_full']}' )";
		
		return mysql_query($sql, dbconnect());
	}
	function InsertProductRefer($arr)
	{
		$sql = "INSERT INTO `product_refer_tb` 
							(`product_id`, `product_refer_name`, `product_refer_price`, `product_refer_img`)
					VALUE	({$arr['product_id']}, '{$arr['product_refer_name']}', {$arr['product_refer_price']}, '{$arr['product_refer_img']}')";
		
		return mysql_query($sql, dbconnect());
	}
	
	function DeleteProduct($product_id){
		return mysql_query("DELETE FROM `product_tb` WHERE `product_id`= $product_id", dbconnect());
	}
	function DeleteProductRefer($product_refer_id){
		return mysql_query("DELETE FROM `product_refer_tb` WHERE `product_refer_id`= $product_refer_id", dbconnect());
	}
	function EditProduct($product_id, $arr)
	{
		$arr['product_des_short'] = addslashes($arr['product_des_short'] );
		$arr['product_des_full'] = addslashes($arr['product_des_full'] );
		$arr['product_name'] = addslashes($arr['product_name'] );
		$sql = "UPDATE `product_tb` 
						SET `product_name`='{$arr['product_name']}', 
							`type_id` = {$arr['type_id']},
							`brand_id` = {$arr['brand_id']},
							`product_img1`   = '{$arr['url1']}',
							`product_img2`   = '{$arr['url2']}',
							`product_img3`   = '{$arr['url3']}',
							`product_img4`   = '{$arr['url4']}',
							`product_img5`   = '{$arr['url5']}',
							`product_des_short`='{$arr['product_des_short']}', 
							`product_des_full`='{$arr['product_des_full']}',
							`product_date` = `product_date`
						WHERE `product_id`=$product_id";
		
		mysql_query($sql, dbconnect());
	}
	
	function GetProductReferByReferId($refer_id)
	{
		$sql = mysql_query("SELECT * FROM `product_refer_tb`
							WHERE 	`product_refer_tb`.`product_refer_id` = $refer_id", dbconnect());
		
		$result = mysql_fetch_assoc($sql);
		return $result;
	}
		
	function UpdatePrice($id, $price, $priceSale)
	{
		return mysql_query("UPDATE `product_tb` 
							SET `product_price`=$price, `product_price_sale`=$priceSale, `product_date` = `product_date`
							WHERE `product_id`=$id", dbconnect());
		
	}
	function UpdateProductReferStatus($id, $status)
	{
		return mysql_query("	UPDATE `product_refer_tb` 
									SET `product_refer_status`=$status
									WHERE `product_refer_id`=$id", dbconnect());
	}
	function UpdateProductOrder($product_id, $adj)
	{
		if ($adj == '+')
			$sql = "UPDATE `product_tb` 
							SET `product_order`=`product_order` + 1 , `product_date` = `product_date`
							WHERE `product_id`=$product_id";
		else
			$sql = "UPDATE `product_tb` 
							SET `product_order`=`product_order` - 1 , `product_date` = `product_date`
							WHERE `product_id`=$product_id";
		return mysql_query($sql, dbconnect());
	}
	function UpdateProductReferOrder($product_refer_id, $adj)
	{
		if ($adj == '+')
			$sql = "UPDATE `product_refer_tb` 
							SET `product_refer_order`=`product_refer_order` + 1 
							WHERE `product_refer_id`=$product_refer_id";
		else
			$sql = "UPDATE `product_refer_tb` 
							SET `product_refer_order`=`product_refer_order` - 1 
							WHERE `product_refer_id`=$product_refer_id";
		return mysql_query($sql, dbconnect());
	}
	function UpdateProductHot($product_id, $adj)
	{
		if ($adj == 'hot')
			$sql = "UPDATE `product_tb` 
							SET `product_status`= 2 , `product_date` = `product_date`
							WHERE `product_id`=$product_id";
		else
			$sql = "UPDATE `product_tb` 
							SET `product_status`= 1
							WHERE `product_id`=$product_id";
		return mysql_query($sql, dbconnect());
	}
	function UpdateProductPrice($product_refer_id)
	{
		$refer = GetProductReferByReferId($product_refer_id);
		$product_id = $refer['product_id'];
		
		$referArr = GetProductReferById($product_id);
		
		foreach ($referArr as $value)
		{
			if($value['product_refer_id'] == $product_refer_id)
				UpdateProductReferStatus($value['product_refer_id'], 2);
			else
				UpdateProductReferStatus($value['product_refer_id'], 1);
		}
		
		if(UpdatePrice($product_id, $refer['product_refer_price'], $refer['product_refer_price_sale']))
			return $refer['product_refer_price'];
		else
			return false;
	}
	function UpdateProductRefer($id, $newName, $newPrice, $newPriceSale, $newPalette)
	{
		$newName = mysql_real_escape_string($newName);
		$sql = "UPDATE 	`product_refer_tb` 
							SET 	`product_refer_name`='$newName',
									`product_refer_img`='$newPalette',
									`product_refer_price`=$newPrice,
									`product_refer_price_sale`=$newPriceSale
							WHERE 	`product_refer_id`=$id";
		
		//var_dump($sql);die();
		return mysql_query($sql, dbconnect());
	}
	
	function UpdatePalette($id, $palette)
	{
		return mysql_query("UPDATE `product_tb` 
							SET `product_palette_img`= '$palette'
							WHERE `product_id`=$id", dbconnect());
		
	}
	function GetNumProductHot()
	{
		$sql = "SELECT count(product_id) as num FROM `product_tb`
				WHERE `product_status` = 2";
				
		
		$sql = mysql_query($sql, dbconnect());
		$result = mysql_fetch_assoc($sql);
		return $result['num'];
	}
	function GetProductHot( $curPage=null, $rowPerPage=null)
	{
		if($curPage && $rowPerPage)
		{
			if($curPage > 0)
			$curPage--;
			$from = $curPage * $rowPerPage;
			
			$sql = mysql_query("SELECT * FROM `product_tb` 
								WHERE `product_tb`.`product_status` = 2
								ORDER BY `product_order` DESC
								LIMIT $from, $rowPerPage", dbconnect());
			$result = array(); 
			while ($product_row = mysql_fetch_assoc($sql))  
				$result[] = $product_row;

			return $result;
			
		}
		else
		{
			$sql = mysql_query("SELECT * FROM `product_tb` 
								WHERE `product_tb`.`product_status` = 2", dbconnect());
			$result = array(); 
			while ($product_row = mysql_fetch_assoc($sql))  
				$result[] = $product_row;

			return $result;
		}
	}
	function GetProductNew()
	{
		$sql = mysql_query("SELECT * FROM `product_tb` 
							ORDER BY `product_tb`.`product_date` DESC
							LIMIT 0, 9", dbconnect());
		$result = array(); 
		while ($product_row = mysql_fetch_assoc($sql))  
			$result[] = $product_row;

		return $result;
	}
	function GetProductSaleOff($curPage=null, $rowPerPage=null)
	{
		if($curPage && $rowPerPage)
		{
			if($curPage > 0)
			$curPage--;
			$from = $curPage * $rowPerPage;
			
			$sql = mysql_query("SELECT * FROM `product_tb` 
								WHERE `product_price_sale` > 0
								ORDER BY `product_order` DESC
								LIMIT $from, $rowPerPage", dbconnect());
			$result = array(); 
			while ($product_row = mysql_fetch_assoc($sql))  
				$result[] = $product_row;

			return $result;
		}
		else
		{
		
			$sql = mysql_query("SELECT * FROM `product_tb` 
								WHERE `product_price_sale` > 0", dbconnect());
			$result = array(); 
			while ($product_row = mysql_fetch_assoc($sql))  
				$result[] = $product_row;

			return $result;
		
		}
	}
	function GetNumProductSaleOff()
	{
		$sql = "SELECT count(product_id) as num FROM `product_tb`
				WHERE `product_price_sale` > 0";
		
		$sql = mysql_query($sql, dbconnect());
		$result = mysql_fetch_assoc($sql);
		return $result['num'];
	}
?>
